package com.dream.keigezhushou.Activity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Fragment.PlayFragment;
import com.dream.keigezhushou.Activity.Uiutils.ToastUtils;
import com.dream.keigezhushou.Activity.bean.GameListBean;
import com.dream.keigezhushou.Activity.bean.Image;
import com.dream.keigezhushou.Activity.view.NineGridLayout;
import com.dream.keigezhushou.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import uk.co.senab.photoview.PhotoView;

/**
 * 玩法的Adapter
 */

public class GameAdapter extends RecyclerView.Adapter<GameAdapter.MyViewHolder>{


    private Context mContext;
    private List<GameListBean> datalist;
    private NineGridAdapter adapter;
    private PlayFragment.OnItemClickListener myItemClickListener;
    private GameListBean gameListBean;
    private List<GameListBean.CoverBean> itemList;

    public GameAdapter(Context context, List<GameListBean> datalist) {

        this.mContext = context;
        this.datalist = datalist;
    }


    public void setOnItemClickListener(PlayFragment.OnItemClickListener myItemClickListener) {
        this.myItemClickListener = myItemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_pic_item,parent,false);
        MyViewHolder mHolder = new MyViewHolder(view);
        return mHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        gameListBean = datalist.get(position);

        itemList = datalist.get(position).getCover();
        if (itemList.isEmpty() || itemList.isEmpty()){

            holder.nineLayout.setVisibility(View.GONE);
        }else {
            holder.nineLayout.setVisibility(View.VISIBLE);
            handlerOneImage(holder, itemList);
        }

        holder.tvUserNick.setText(gameListBean.getNickname());
        holder.tvDate.setText(gameListBean.getDateline());
        holder.tvDesc.setText(gameListBean.getContent());
        holder.tvEvaluate.setText(gameListBean.getComments());

        if (myItemClickListener != null){

            holder.tvEvaluate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    myItemClickListener.onEvaluateClick(position);
                }
            });

            holder.tvShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    myItemClickListener.onShareClick(position);
                }
            });
        }
    }

    private void handlerOneImage(MyViewHolder holder, List<GameListBean.CoverBean> itemList) {
        adapter = new Adapter(mContext, itemList);
        holder.nineLayout.setAdapter(adapter);
        holder.nineLayout.setOnItemClickListerner(new NineGridLayout.OnItemClickListerner() {
            @Override
            public void onItemClick(View view, int position) {
                //do some thing
                ToastUtils.showToast(mContext,"点击了图片");
            }
        });
    }

    @Override
    public int getItemCount() {
        return (datalist == null) ? 0 : datalist.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{


        ImageView ivUserPic;
        TextView tvUserNick;
        TextView tvDate;
        TextView tvDesc;
        TextView tvEvaluate;
        TextView tvShare;
        NineGridLayout nineLayout;

        public MyViewHolder(View itemView) {
            super(itemView);

            ivUserPic = (ImageView)itemView.findViewById(R.id.iv_user_pic);
            tvUserNick = (TextView)itemView.findViewById(R.id.tv_user_nick);
            tvDate = (TextView)itemView.findViewById(R.id.tv_date);
            tvDesc = (TextView)itemView.findViewById(R.id.tv_describe);
            tvEvaluate = (TextView)itemView.findViewById(R.id.tv_evaluate);
            tvShare = (TextView)itemView.findViewById(R.id.tv_share);
            nineLayout = (NineGridLayout)itemView.findViewById(R.id.nine_grid_layout);

        }
    }

    //创建九宫格输出的适配器
    class Adapter extends NineGridAdapter {

        public Adapter(Context context, List list) {
            super(context, list);
        }

        @Override
        public int getCount() {
            return (list == null) ? 0 : list.size();
        }

        @Override
        public String getUrl(int position) {
            return getItem(position) == null ? null : ((GameListBean.CoverBean)getItem(position)).getCover();
        }

        @Override
        public Object getItem(int position) {
            return (list == null) ? null : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View view) {
                ImageView iv = null;
            if (view != null && view instanceof ImageView) {
                iv = (ImageView) view;
            } else {
                iv = new ImageView(context);
            }
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setBackgroundColor(context.getResources().getColor((android.R.color.transparent)));
            String url = gameListBean.getCover().toString();
            Picasso.with(context).load(getUrl(i)).placeholder(new ColorDrawable(Color.parseColor("#f5f5f5"))).into(iv);
            if (!TextUtils.isEmpty(url)) {
                iv.setTag(url);
            }
            return iv;
        }
    }
}


